﻿/**
 * @file singleton.hpp
 * @date 2021/04/21 10:05
 *
 * @author harrybao (harrybao@123u.com)
 *
 * @brief 单例模板
 *
 * @version v1.0.0
 *
 * @copyright Copyright © 2021 All Rights Reserved 上海欢乐互娱网络科技有限公司
 *
 * @par history:
 * <table>
 * <tr><th>Date                     <th>Version     <th>Author <th>Description
 * <tr><td>2021/04/21 10:05         <td>1.0.0       <td>harrybao
 * <td>创建初始版本
 * </table>
 */

// #ifndef SINGLETON_HPP
// #define SINGLETON_HPP

#pragma once

#include <utility>

namespace guild_service {
namespace common {

/**
 * @brief 单例模板类
 *
 */
template <typename T>
class singleton {
public:
  singleton() = delete;
  singleton(const singleton&) = delete;
  singleton(singleton&&) = delete;
  singleton<T>& operator=(const singleton&) = delete;

public:
  /**
   * @brief 
   * 
   * 非线程安全
   * @tparam Args 
   * @param args 
   * @return T* 
   */
  template <typename... Args>
  static T* instance(Args&&... args) {
    if (!instance_) {
      instance_ = new T(std::forward<Args>(args)...);
    }
    return instance_;
  }

  static void destroy() {
    if (instance_) {
      delete instance_;
    }
    instance_ = nullptr;
  }

private:
  static T* instance_;
};

template <typename T>
T* singleton<T>::instance_ = nullptr;

}  // namespace common
}  // namespace guild_service

// #endif  // SINGLETON_HPP